package models

import (
	"GoAdmin/models/response"
	"time"
)

type OaRole struct {
	Id          int       `xorm:"not null pk autoincr comment('自增主键') INT(11)"`
	CreatorId   int       `xorm:"comment('创建者id，user_id外键关联oa_user中的id') index INT(11)"`
	Name        string    `xorm:"default '' comment('角色名') VARCHAR(32)"`
	Description string    `xorm:"default '' comment('角色特点描述信息') VARCHAR(128)"`
	CreateTime  time.Time `xorm:"not null comment('角色创建时间') DATETIME created"`
	RoleAction  string    `xorm:"comment('角色权限列表  以,分隔') TEXT"`
	Status      string    `xorm:"comment('') VARCHAR(11)"`
	EditTime    string    `xorm:"not null comment('角色修改时间')"`
}

func (o *OaRole) GetAllRoleList(pagesize, offset int) (count int64, list []*response.RoleList, err error) {
	err = engine.Table("oa_role").Alias("r").
		Select("r.id,r.name,r.create_time,r.description,u.name as creator").
		Join("LEFT", []string{"oa_user", "u"}, "u.id = r.creator_id").Where("r.status = ?", "1").
		Limit(pagesize, offset).Desc("r.id").Find(&list)
	count, _ = engine.Table("oa_role").Alias("r").
		Join("LEFT", []string{"oa_user", "u"}, "u.id = r.creator_id").Where("r.status = ?", "1").
		Count()
	// log.Println(err, list)
	return
}

func (o *OaRole) GetById(id string) (has bool, list OaRole, err error) {
	has, err = engine.Table("oa_role").
		Where("id = ?", id).
		Get(&list)
	return
}

func (o *OaRole) GetByUserId(id int) (has bool, list OaRole, err error) {
	has, err = engine.Table("oa_role").
		Where("used_id = ?", id).
		Get(&list)
	return
}

func (o *OaRole) Edit(fields ...string) bool {
	var (
		err error
	)
	_, err = engine.Id(o.Id).Cols(fields...).Update(o)
	if err != nil {
		return false
	}
	return true
}

func (o *OaRole) Add() bool {
	if _, err := engine.Table("oa_role").Insert(o); err != nil {
		return false
	}
	return true
}

// 角色列表

//func (o *OaRole) Delete() bool {
//	if _, err := engine.Table("oa_role").Where("id=?", o.Id).Delete(new(OaRole)); err != nil {
//		log.Println(err)
//		return false
//	}
//	return true
//
//}
